package com.slz3.business.module.systemManagement.sysPermissions.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.systemManagement.sysPermissions.domain.SysPermissions;
import com.slz3.business.module.systemManagement.sysPermissions.domain.SysPermissionsFactory;
import com.slz3.business.module.systemManagement.sysPermissions.steward.SysPermissionsSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class SysPermissionsServiceImpl implements SysPermissionsService {

    @Override
    public SysPermissions save(SysPermissions sysPermissions) {
        return Repositorys.sysPermissions.save(SysPermissionsFactory.initBaseFields(sysPermissions));
    }

    @Override
    public Page<SysPermissions> page(MultiValueMap<String, String> params) {
        Page<SysPermissions> sysPermissionsPage = Repositorys.sysPermissions.findAll(
                SysPermissionsSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return sysPermissionsPage;
    }

    @Override
    public SysPermissions modify(SysPermissions sysPermissionsNew) {
        return Repositorys.sysPermissions.saveAndFlush(SysPermissionsFactory.initBaseFields(sysPermissionsNew));
    }

    @Override
    public Optional<SysPermissions> single(UUID uuid) {
        return Repositorys.sysPermissions.findById(uuid);
    }

    @Override
    public List<SysPermissions> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.sysPermissions.findAllById(uuids);
    }

    @Override
    public List<SysPermissions> delete(List<SysPermissions> sysPermissionsList) {
        sysPermissionsList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            SysPermissionsFactory.initBaseFields(item);
        });
        return Services.sysPermissionsTransactional.batchModify(sysPermissionsList);
    }

    @Override
    public boolean hasAuth(UUID loginUserUuid, String path) {
        return Repositorys.sysPermissions.hasAuth(loginUserUuid, path) > 0;
    }
}
